@echo off
REM ============================================
REM Worker Setup Script (Windows)
REM ============================================

chcp 65001 >nul

REM Save current directory and change to project root
set SCRIPT_DIR=%~dp0
cd /d "%SCRIPT_DIR%\..\.."

echo ==================================
echo   Worker Setup Script
echo ==================================
echo.

REM Check Python
echo [1/5] Checking Python...
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found. Please install Python 3.8+
    pause
    exit /b 1
)
python --version
echo [OK] Python installed
echo.

REM Check pip
echo [2/5] Checking pip...
pip --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] pip not found
    pause
    exit /b 1
)
echo [OK] pip installed
echo.

REM Ask about virtual environment
echo [3/5] Installing dependencies...
set /p use_venv="Use virtual environment? (recommended) [Y/n]: "
if "%use_venv%"=="" set use_venv=Y

if /i "%use_venv%"=="Y" (
    if not exist "venv" (
        echo Creating virtual environment...
        python -m venv venv
    )
    echo Activating virtual environment...
    call venv\Scripts\activate.bat
    echo [OK] Virtual environment activated
)

REM Install dependencies
echo Installing Python packages (this may take a few minutes)...
pip install -r requirements.txt --upgrade
echo [OK] Dependencies installed
echo.

REM Create directories
echo [4/5] Creating directories...
if not exist "logs" mkdir logs
if not exist "cache" mkdir cache
if not exist "output" mkdir output
echo [OK] Directories created
echo.

REM Check config files
echo [5/5] Checking config files...
if not exist "config\worker_config.json" (
    if exist "config\worker_config.example.json" (
        copy config\worker_config.example.json config\worker_config.json >nul
        echo [WARN] Created config file: config\worker_config.json
        echo        Please run: scripts\windows\config_wizard.bat
    ) else (
        echo [ERROR] Example config file not found
        pause
        exit /b 1
    )
) else (
    echo [OK] Config file exists
)

if not exist ".env" (
    if exist "config\.env.example" (
        copy config\.env.example .env >nul
        echo [WARN] Created .env file
        echo        For AI polish, add DeepSeek API Key to .env
    )
) else (
    echo [OK] .env file exists
)

REM Complete
echo.
echo ==================================
echo   Setup Complete!
echo ==================================
echo.
echo Next steps:
echo.
echo   1. Configure Worker (first time):
echo      scripts\windows\config_wizard.bat
echo.
echo   2. Start Worker:
echo      scripts\windows\start_worker.bat --excel C:\path\to\excel.xlsx
echo.
echo   3. View help:
echo      scripts\windows\start_worker.bat --help
echo.

if /i "%use_venv%"=="Y" (
    echo Note: After closing terminal, activate venv first:
    echo      venv\Scripts\activate.bat
    echo.
)

pause
